package com.example.controller.backend;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.ApiResult;
import com.example.entity.Evaluate;
import com.example.service.EvaluateService;
import com.example.vo.EvaluateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.internal.properties.Getter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.PublicKey;
import java.util.List;

/**
 * 后端管理
 */
@RestController//不加在Postman中无法用JSON测试
@Api(tags = "评价表接口")//swagger页面提示
public class EvaluateController {
    /*Get  @GetMapping  查询
    Post @PostMapping 增、删、改、查
    Put  @PutMapping  修改
    Delete @DeleteMapping 删除
    @RequestMapping  接收所有的请求方式（GET\POST\PUT\DELETE）
   */

    @Resource
    private EvaluateService evaluateService;

    /*
paramType:
 1、@RequestParam     paramType="query"
 2、@PathVariable     paramType="path"
 3、 @RequestBody     paramType="body/form"
     */


    /**
     * 分页查询语句
     * http://localhost:8080/PagedQuery/1/2
     * @param currentPage 当前页数
     * @param pageSize  一页显示多少条
     * @return
     */

    @ApiOperation("分页查询全部")//swagger接口注释
    @GetMapping("/PagedQuery/{currentPage}/{pageSize}")
    public ApiResult<IPage<Evaluate>> getPage(@PathVariable int currentPage, @PathVariable int pageSize){
        IPage<Evaluate> page = evaluateService.getPage(currentPage, pageSize);
        LambdaQueryWrapper<Evaluate>queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据时间降序
        queryWrapper.orderByDesc(Evaluate::getCreateDate);
        //执行查询
        evaluateService.page(page, queryWrapper);
        return ApiResult.ok(page);
    }



    /**
     * 联合查询
     * http://localhost:8080/AssociatedQuery/1
     * @param id
     * @return
     */
    @ApiOperation("联合查询")//swagger接口注释
    @GetMapping("/AssociatedQuery/{id}")
    public ApiResult AssociatedQuery(@PathVariable Integer id){
        List<EvaluateVo> lianhe = evaluateService.lianhe(id);
        return ApiResult.ok(lianhe);
    }




    /**
     * 查询全部
     * http://localhost:8080/queryXY
     * @return
     */
    @ApiOperation("查询全部")//swagger接口注释
    @GetMapping("/queryXY")
    public ApiResult<List<Evaluate>> query(){
        List<Evaluate> list =evaluateService.list();
        return ApiResult.ok(list);
    }



    /**
     * 删除
     * http://localhost:8080/deleteXY/4
     * @param id
     * @return
     */
    @ApiOperation("删除")//swagger接口注释
    @PostMapping("/deleteXY/{id}")
    public ApiResult<Boolean> delete(@PathVariable Integer id){
        boolean b = evaluateService.removeById(id);
        return ApiResult.ok(b);
    }


    /**
     * 添加
     *  http://localhost:8080/addXY
     *  {"userId":"2","courseId":"1","content":"根据计划,"starCount":"1"}
     * @param evaluate
     * @return
     */
    @ApiOperation("添加")//swagger接口注释
    @PostMapping("/addXY")
    public ApiResult addEval(@RequestBody Evaluate evaluate){
        evaluateService.save(evaluate);
        return ApiResult.ok(evaluate);
    }


    /**
     * 修改
     * http://localhost:8080/modifyXY
     * @param evaluate
     * @return
     */
    @ApiOperation("修改")//swagger接口注释
    @PutMapping("/modifyXY")
    public ApiResult modify(@RequestBody Evaluate evaluate){
        evaluateService.updateById(evaluate);
        return ApiResult.ok(evaluate);
    }

    /**
     * 模糊查询
     * @param FuzzyEva
     * @return
     */
    @ApiOperation("模糊查询")//swagger接口注释
    @GetMapping("/FuzzyQuery")
    public ApiResult<List<Evaluate>> fuzzyQuery(String FuzzyEva) {
        List<Evaluate> list =  evaluateService.FuzzyQuerY(FuzzyEva);
        return ApiResult.ok(list);

    }
}
